﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MBSBusinessEntities;
using System.Transactions;
using MBSProcessComponent;
namespace MBSBLC
{
    public partial class BLC
    {
        #region Booking
        BookingBE bookingbe;

        public int AddBooking(DateTime bookingDate, string seatnumbers, int showTimeID, int userID)
        {
            int BookingID = 0;
            //Fetch Seatnumbers and UserId from Session
            bookingbe = new BookingBE(bookingDate, seatnumbers, showTimeID, userID);
            String[] seatArray = bookingbe.Seatnumbers.Split(',');
            List<String> seatList = seatArray.ToList<String>();
            showTimeBE = GetShowTimeBEByID(showTimeID);
            // bookingbe=new BookingBE(bookingDate,seatnumbers,showTimeID,userID);
            using (TransactionScope tran = new TransactionScope())
            {
                ShowTimeBE showtimebe = (ShowTimeBE)showTimeBE.GetByID(showTimeID);
                // Get Rewards Points

                int points = bookingbe.getRewardPointsGained(seatnumbers);
                bookingbe.RewardPointsGained = points;
                //Do booking
                BookingID = bookingbe.AddBooking();
                
                //If User is registered Add Rewards Point in his account.
                if (userID > 0)
                {
                    UserBE userBE = new UserBE(userID);
                    if (userBE.IsValid)
                    {
                        userBE.AddRewardsPoint(points);
                    }

                }

                //Update Available seats in ShowTime
                showTimeBE.SeatsAvailable = BLCUtility.setSeatStatusBooked(showTimeBE.SeatsAvailable, seatList);
                showTimeBE.updateSeatAvailable();
                //Update Seat in Cache
                bool cacheStatus = bookAvailableSeatsInCache(showTimeID, seatList);
                tran.Complete();
            }
            return BookingID;
        }

        public Dictionary<int, string> GetBookings(int userID)
        {
            BookingBE bookingbe = new BookingBE();
            List<BookingBE> bookingbeList = bookingbe.getUserBookings(userID);
            Dictionary<int, string> bookingDictionary = new Dictionary<int, string>();
            foreach (BookingBE bookingBETemp in bookingbeList)
            {
                string bookingdt = bookingBETemp.BookingDate.ToString();
                bookingDictionary.Add(bookingBETemp.BookingID, bookingdt);
            }
            return bookingDictionary;
        }

        public object GetByID(int id)
        {
            BookingBE objBookingBE = new BookingBE();
            return objBookingBE.GetByID(id);
        }

        public int getBookingID(int showtimeid, int userid)
        {
            BookingBE bookingbe = new BookingBE();
            int bookID = bookingbe.getBookingID(showtimeid, userid);
            return bookID;
        }

        #endregion
    }
}
